<%layout("/common/_layout.html"){ %>
<div>
    <% include("/admin/waiting_hint.tpl"){}%></div>
<div class="am-cf am-padding">
    <div class="am-fl am-cf"> <strong class="am-text-primary am-text-lg">业务管理</strong>
        /
        <small>白卡管理</small>
    </div>
</div>
<form id="list-form" action="${base}/blankcard/list" method="post">
    <input type="hidden" id="keywords" name="keywords" value='${keywords!""}' />
    <table class="am-table am-table-bordered am-table-radius table-main">
        <tbody>
            <tr>
                <th class="am-text-middle">所属机构</th>
                <td>
                    <div class="am-u-sm-10">
                        <select data-am-selected="{btnSize:'sm',  maxHeight: 400}" name="org_id" id="org_id">
                            <%for(orgNode in orgList){%>
                            <option value="${orgNode.value}" <%if(org_id==orgNode.value){%>
                                selected
                                <%}%>>${orgNode.key}</option>
                            <%}%></select>
                    </div>
                </td>
                <th class="am-text-middle">渠道编码</th>
                <td>
                    <div class="am-u-sm-10">
                        <input type="text" class="am-form-field am-input-sm" name="number_code"  id="number_code" value="${number_code!}" />
                    </div>
                </td>
                <th class="am-text-middle">机构名称</th>
                <td>
                    <div class="am-u-sm-10">
                        <input type="text" class="am-form-field am-input-sm" name="org_name"  id="org_name" value="${org_name!}" />
                    </div>
                </td>
                <td >
                    <div align='right'>
                        <button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" id="submit-btn">搜索</button>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
    <div class="am-g">
        <div class="am-u-sm-12 am-u-md-6" style="padding:0px 1.6rem 1.6rem 1rem;margin:0px;">
            <div class="am-btn-toolbar">
                <div class="am-btn-group am-btn-group-xs">
                    <%if(so.hasPermission("BLANKCARD_UPLOAD")) {%>
                    <div class="am-btn-group am-btn-group-xs am-form-file">
                        <button type="button" id="bt_import" class="am-btn am-btn-default">
                            <span class="am-icon-archive"></span>
                            导入
                        </button>
                        <input id="doc-form-file" type="file" name="import" onChange="ajaxUploadFile('doc-form-file','${base}/blankcard/upload')" multiple></div>
                    <%}%></div>
                <div class="am-dropdown" data-am-dropdown>
                    <button id="templateDownload" type="button" class="am-btn am-btn-default am-btn-xs am-dropdown-toggle" data-am-dropdown-toggle>
                        <span class="am-icon-download"></span>
                        模板
                    </button>
                </div>
            </div>
        </div>
    </form>
    <div class="am-g">
        <div class="am-u-sm-12">
            <table class="am-table am-table-striped am-table-hover table-main">
                <thead>
                    <tr>
                        <th class="table-title">机构名称</th>
                        <th class="table-title">买断总量</th>
                        <th class="table-title">写卡量</th>
                        <th class="table-title">白卡剩余量</th>
                        <%if(so.hasPermission("BLANKCARD_UPDATE")) {%>
                        <th class="table-title">白卡新增</th>
                        <th class="table-title">操作</th>
                        <%}%></tr>
                </thead>
                <tbody >
                    <%for(list in pager.list){%>
                    <tr>
                        <td>${list.DisplayName}</td>
                        <td>${list.SIMCardCount!0}</td>
                        <td>${list.used}</td>
                        <td>${list.freeCount!}</td>
                        <%if(so.hasPermission("BLANKCARD_UPDATE")) {%>
                        <td>
                            <input type="text" class="am-input-sm" style="width:40%" placeholder="0" />
                        </td>
                        <td>
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="updateWhiteCard($(this), ${list.orgId});">
                                        <span class="am-icon-save"></span>
                                        保存
                                    </button>
                                </div>
                            </div>
                        </td>
                        <%}%></tr>
                    <%}%></tbody>
            </table>
            <div class="am-cf">
                <!-- 分页 -->
                <%if (pager.list.~size >
                0){%>
                <div class="am-fr">
                    <% include("/admin/common_pager.tpl"){}%></div>
                <%}else{%>
                <div class="am-kai">
                    <h3>没有找到任何记录!</h3>
                </div>
                <%}%></div>
        </div>
    </div>
    <div class="am-modal am-modal-no-btn" tabindex="-1" id="modal">
        <div class="am-modal-dialog">
            <div class="am-modal-hd"> <i class="am-icon-refresh am-icon-spin"></i>
                <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
            </div>
            <div class="am-modal-bd">正在处理，请稍等...</div>
        </div>
    </div>

    <script type="text/javascript">
        var urlBase = "${base}";
        var url;

        $(function(){
            var keywordsObj = {};
            $("#submit-btn").on("click",function(){
                if($("#org_id").val())
                    keywordsObj.org_id = $("#org_id").val();
                if($("#number_code").val())
                    keywordsObj.number_code = $("#number_code").val();
                if($("#org_name").val())
                    keywordsObj.org_name = $("#org_name").val();

                var keywords = "";
                if(!$.isEmptyObject(keywordsObj)){
                  keywords = JSON.stringify(keywordsObj);
                }
                console.log("keywords = "+ keywords);
                $("#keywords").val(keywords);
                showWaitingHint("正在查询，请稍候...");
                $("#list-form").submit();
            });
            $("#templateDownload").on('click', function(event) {
                //alert('下载模版');
                location.href = "${base}/blankcard/template";
            });
        });

        function updateWhiteCard(obj, orgId){
            var inputAdd = obj.parent().parent().parent().prev().children("input");
            var tdTotalNum = obj.parent().parent().parent().prev().prev().prev().prev();
            var tdAvailableNum = obj.parent().parent().parent().prev().prev();
            var availableNum = tdAvailableNum.text();
            var addNum = inputAdd.val();
            if(addNum.length == 0){
                showDialog("", "白卡新增数量不能为空！", "info", 2000);
                return;
            }
            if(availableNum.length==0){
                availableNum = 0;
                return;
            }
            if(!isInt(addNum)){
                showDialog("", "白卡新增数量输入格式不正确，请输入数字！", "info", 2000);
                return;
            }
            if(parseInt(addNum)==0){
                showDialog("", "白卡新增数量不能为0！", "info", 2000);
                return;
            }
            if(parseInt(addNum)+parseInt(availableNum)<0){
                showDialog("", "白卡减少量不能小于白卡剩余量！", "info", 2000);
                return;
            }
            showWaitingHint("修改白卡数量...");
            $.ajax({
                url : urlBase + "/blankcard/update",
                type : 'POST',
                dataType : 'json',
                data : {
                    org_id : orgId, add_num : addNum
                },
                cache : false
            }).done(function (result) {
                if (result) {
                    if(result.status==200){
                        inputAdd.val("");
                        tdTotalNum.text(result.total_num);
                        tdAvailableNum.text(result.available_num);
                        showDialog("操作成功", result.message, "success", 2000);
                    }else if(result.status == 401){
                            showDialog("操作失败", result.message, "error", 2000);
                            setTimeout(directToLoging(result.url), 3000);
                    }else{
                        showDialog("操作失败", result.message, "error", 2000);
                    }
                    dismissWaitingHint();
                }
            }).fail(function () {
                showDialog("操作失败", "", "error", 2000);
                dismissWaitingHint();
            });
        }

        function directToLoging(url){
            top.location.href = urlBase + url;
        }

        function showDialog(msgTitle, message, msgType, msgTimer){
            sendMessage({
                            title : msgTitle,
                            text : message,
                            type : msgType,
                            timer : msgTimer,
                            confirmButtonText : false
            });
        }

        function isInt(data) {
            var result = false;
            if (typeof data != 'undefined') {
              // 正则表达式 匹配 判断是负数 0 正数
              //var regexp = /^\d*$/;
              var regexp =  /^[+-]?\d*$/;
              if (regexp.test(data)) {
                  result = true;
              } else {
                  result = false;
              }
            }
            return result;
        }
    </script>
    <%}%>